package com.sysu.minsysu.zdkb;

import java.util.ArrayList;
import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.Time;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

import com.sysu.minsysu.R;
import com.sysu.minsysu.zdkb.utils.Course;
import com.sysu.minsysu.zdkb.utils.CourseUtils;

public class FindClassRooms extends Activity{

	private static final String[] CAMPUSNAMES = new String[]{
		"东校区", "南校区", "北校区", "珠海校区"
	};
	
	private static final String[][] BUILDINGNAME = new String[][]{
		{"教学楼A", "教学楼B", "教学楼C", "教学楼D", "教学楼E"},
		{"正在完善中...."},
		{"正在完善中...."},
		{"正在完善中...."},
	};
	
	private static final String[] CLASSTEXT = new String[16];
	
	private static final String[] CLASSTIMEBEGINSTRINGS = new String[16];
	
	private static final String[] CLASSTIMEENDSTRINGS = new String[16]; 
	
	private static final String[] WEEKDAYCHARACTER = new String[]{
		"日", "一", "二", "三", "四", "五", "六"
	};
	
	Spinner mCampuSpinner;
	Spinner mBuildingSpinner;
	Button mTimeBeginButton;
	Button mTimeEndButton;
	Button mDateButton;
	Button mComfireButton;
	Button mBackButton;
	
	int mYear;
	int mMonth;
	int mDay;
	int mDayOfWeek;
	int mHourStart;
	int mMinuteStart;
	int mHourEnd;
	int mMinuteEnd;
	
	int mClassBegin;
	int mClassEnd;
	
	@Override
	public void onCreate( Bundle savedInstanceState ){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.findclassroom);
		prepare();
		setupViews();
	}
	
	private void prepare(){
		for( int i=1; i<=16; i++ ){
			CLASSTEXT[i-1] = "第" + i + "节";
		}
		for( int i=0; i<16; i++ ){
			Time t1 = CourseUtils.classTimeConverter(i,false);
			CLASSTIMEBEGINSTRINGS[i] = t1.hour + ":" + String.format("%02d", t1.minute); 
			Time t2 = CourseUtils.classTimeConverter(i, true);
			CLASSTIMEENDSTRINGS[i] = t1.hour + ":" + String.format("%02d", t2.minute);
		}
	}
	
	private int getNowClassTime(){
		Calendar calendar = Calendar.getInstance();
		int hour = calendar.get(Calendar.HOUR_OF_DAY) - 8;
		if( hour<0)return 0;
		int minute = calendar.get(Calendar.MINUTE);
		int now = hour*60+minute;
		return now/55>15 ? 0 : now/55;
	}
	
	private String dateTextFormater( int year, int month, int day ){
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month, day);
		return  year+"-"+month+"-"+day+" 周" + WEEKDAYCHARACTER[ calendar.get(Calendar.DAY_OF_WEEK)-1 ];
	}
	
	private String timeTextFormater( int i ){
		return CLASSTEXT[i] +" "+ CLASSTIMEBEGINSTRINGS[i];
	}
	
	private void setupViews(){
		mCampuSpinner = (Spinner)findViewById(R.id.campus);
		mBuildingSpinner = (Spinner)findViewById(R.id.teaching_building);
		mTimeBeginButton = (Button)findViewById(R.id.time_begin);
		mTimeEndButton = (Button)findViewById(R.id.time_end);
		mDateButton = (Button)findViewById(R.id.date);
		mComfireButton = (Button)findViewById(R.id.comfire);
		mBackButton = (Button)findViewById(R.id.back);
		
		mCampuSpinner.setAdapter( new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, CAMPUSNAMES));
		mCampuSpinner.setSelection(0);
		mCampuSpinner.setOnItemSelectedListener( new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				mBuildingSpinner.setAdapter(new ArrayAdapter<String>(FindClassRooms.this, android.R.layout.simple_spinner_item, BUILDINGNAME[position]));
				mBuildingSpinner.setSelection(0);
			}
			@Override
			public void onNothingSelected(AdapterView<?> parent) {
			}
		});
		
		mBuildingSpinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, BUILDINGNAME[0]));
		mBuildingSpinner.setSelection(0);
		
		Calendar nowTime = Calendar.getInstance();
		mYear= nowTime.get(Calendar.YEAR);
		mMonth = nowTime.get(Calendar.MONTH);
		mDay = nowTime.get(Calendar.DATE);
		mDayOfWeek= nowTime.get(Calendar.DAY_OF_WEEK);
		mHourStart = nowTime.get(Calendar.HOUR);
		mMinuteStart = nowTime.get(Calendar.MINUTE);
		mHourEnd = mHourStart + 1;
		mMinuteEnd = mMinuteStart;
		
		mDateButton.setText( dateTextFormater(mYear, mMonth, mDay));

		int nowclasstime = getNowClassTime();
		mClassBegin = mClassEnd = nowclasstime;
		mTimeBeginButton.setText( timeTextFormater(nowclasstime) );
		mTimeEndButton.setText( timeTextFormater(nowclasstime));
		
		mDateButton.setOnClickListener( new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				DatePickerDialog dialog = new DatePickerDialog(FindClassRooms.this, new OnDateSetListener() {
					
					@Override
					public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
						mYear = year; mMonth = monthOfYear+1; mDayOfWeek = dayOfMonth;
						mDateButton.setText(dateTextFormater(year, monthOfYear+1, dayOfMonth));
					}
				} ,mYear, mMonth, mDay);
				dialog.show();
			}
		});
		
		mTimeBeginButton.setOnClickListener( new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				AlertDialog.Builder builder = new AlertDialog.Builder(FindClassRooms.this);
				String[] text = new String[16];
				for( int i=0; i<16; i++ ){
					text[i] = CLASSTEXT[i] + " " + CLASSTIMEBEGINSTRINGS[i];
				}
				builder.setSingleChoiceItems(text, mClassBegin, new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						mClassBegin = which;
						mTimeBeginButton.setText(timeTextFormater(which));
						dialog.dismiss();
					}
				});
				builder.show();
			}
		});
		
		mTimeEndButton.setOnClickListener( new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				AlertDialog.Builder builder = new AlertDialog.Builder(FindClassRooms.this);
				String[] text = new String[16];
				for( int i=0; i<16; i++ ){
					text[i] = CLASSTEXT[i] + " " + CLASSTIMEENDSTRINGS[i];
				}
				builder.setSingleChoiceItems(text, mClassEnd, new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						mClassEnd = which;
						mTimeEndButton.setText(timeTextFormater(which));
						dialog.dismiss();
					}
				});
				builder.show();
			}
		});
		
		mComfireButton.setOnClickListener( new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Calendar calendar = Calendar.getInstance();
				calendar.set(mYear, mMonth, mDay);
				if( !CourseUtils.isInThisTerm(calendar)){
					Toast.makeText(FindClassRooms.this, "所选日期不在本学期内，请重新选择日期", Toast.LENGTH_LONG).show();
					return;
				}
				int campusPos = mCampuSpinner.getSelectedItemPosition();
				if( campusPos!=0 ){
					Toast.makeText(FindClassRooms.this, "目前仅支持东校区，其它校区请等待更新.....", Toast.LENGTH_LONG).show();
					return;
				}
					
				String campus = (String)mCampuSpinner.getSelectedItem();
				String[] buildingNames = BUILDINGNAME[ mCampuSpinner.getSelectedItemPosition()];
				int buildingPos = mBuildingSpinner.getSelectedItemPosition();
				int[] date = new int[]{mYear, mMonth, mDay};
				int[] classtime = new int[]{mClassBegin, mClassEnd};
				ArrayList<Course> courses = CourseUtils.getCourseWithTimeInterval(FindClassRooms.this, calendar.get(Calendar.WEEK_OF_YEAR) , mClassBegin, mClassEnd);
				Intent intent = new Intent(FindClassRooms.this, FindClassRoomResultActivity.class);
				intent.putExtra("courses", courses);
				intent.putExtra("campus", campus);
				intent.putExtra("date", date);
				intent.putExtra("time", classtime);
				intent.putExtra("buildingNames", buildingNames);
				intent.putExtra("building", buildingPos);
				startActivity(intent);
			}
		});
		
		mBackButton.setOnClickListener( new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
	
	
}
